Public Class ChatWindow

    Private WithEvents _connection As IrcConnection
    'Private _nick As String
    'Private _usuario As String
    Private Sub txtInput_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyUp
        If e.KeyCode = Windows.Forms.Keys.Enter Then
            _connection.SendCommand(Me.txtInput.Text)
            Me.txtInput.Text = ""
        End If
    End Sub

    Private Sub txtInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged

    End Sub

    Public Sub New(ByRef connection As IrcConnection)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        _connection = connection

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Delegate Sub SetTextCallback(ByVal text As String)
    Private Sub _connection_dataReceived(ByVal data() As Byte) Handles _connection.dataReceived
        Console.WriteLine("dataReceived fired: " + System.Text.Encoding.ASCII.GetString(data))
        If txtOutput.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf WriteToOutput)
            Me.Invoke(d, New Object() {System.Text.Encoding.ASCII.GetString(data)})
        Else
            WriteToOutput(System.Text.Encoding.ASCII.GetString(data))
        End If

    End Sub
    'Public Property Nick() As String
    '    Get
    '        Return _nick
    '    End Get
    '    Set(ByVal value As String)
    '        _nick = value
    '    End Set
    'End Property
    'Public Property Usuario() As String
    '    Get
    '        Return _usuario
    '    End Get
    '    Set(ByVal value As String)
    '        _usuario = value
    '    End Set
    'End Property
    Sub WriteToOutput(ByVal text As String)
        txtOutput.Text = txtOutput.Text + vbCrLf + text
        txtOutput.Select(txtOutput.Text.Length, 0)
    End Sub
    Sub WriteToOutput(ByVal text As String, ByVal Color As Drawing.Color)
        txtOutput.SelectionColor = Color
        txtOutput.Text = txtOutput.Text + vbCrLf + text
        txtOutput.Select(txtOutput.Text.Length - 1, 1)
    End Sub

    Private Sub ChatWindow_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '_connection.Nick = _nick
        '_connection.Username = _usuario
        _connection.Connect()
    End Sub

    Private Sub _connection_dataSent(ByVal data As String) Handles _connection.dataSent
        WriteToOutput("Sent: " + data, Drawing.Color.Green)
    End Sub
End Class